package com.secsbrain.cmdb.web.controller;

import java.util.Date;
import java.util.Map;

import com.secsbrain.sso.core.SsoHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.ICabinetService;
import com.secsbrain.cmdb.biz.IIdcService;
import com.secsbrain.cmdb.biz.impl.CommonService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.cmdb.dal.model.Cabinet;
import com.secsbrain.cmdb.dal.model.Idc;

/**
 * 机房控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/idc")
public class IdcController{
	
    @Autowired
    IIdcService idcService;
    
    @Autowired
    ICabinetService cabinetService;
    
    @Autowired
    CommonService commonService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Idc params) {
    	Page<Idc> page=new Page<Idc>(0, 20);
    	Page<Idc> result=idcService.selectPage(page, params);
    	
    	Map<Long, String> idclevelMap=commonService.getIdclevelMap();
    	Map<Long, String> supplierMap=commonService.getSupplierMap();
    	Map<Long, String> ispMap=commonService.getIspMap();

    	for(Idc idc:result.getRecords()){
    		idc.setIdclevelName(idclevelMap.get(idc.getTypeId()));
    		idc.setSupplierName(supplierMap.get(idc.getSupplierId()));
    		if(StringUtils.isNotEmpty(idc.getOperatorIds())){
    			if(idc.getOperatorIds().indexOf(",")!=-1){
    				String[] array=idc.getOperatorIds().split(",");
    				StringBuilder builder=new StringBuilder();
    				for(String str:array){
    					builder.append(ispMap.get(Long.parseLong(str))+",");
    				}
    				idc.setIspNames(builder.deleteCharAt(builder.length()-1).toString());
    			}else{
    				idc.setIspNames(ispMap.get(Long.parseLong(idc.getOperatorIds())));
    			}
    		}
    		
    		Cabinet cabinetParam=new Cabinet();
    		cabinetParam.setIdcId(idc.getId());
			int cabinetCount=cabinetService.selectCount(cabinetParam);
    		idc.setCabinetCount(cabinetCount+"");
    	}
    	
    	return new ResponseResult(result);
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody Idc entity) {
    	entity.setCreateTime(new Date());
    	entity.setCreator(SsoHelper.currentUser().getNickname());
    	idcService.insertSelective(entity);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody Idc entity) {
    	entity.setUpdateTime(new Date());
    	entity.setUpdator(SsoHelper.currentUser().getNickname());
    	idcService.updateSelectiveById(entity);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody Idc entity) {
    	idcService.deleteById(entity.getId());
    	return new ResponseResult();
    }
    
}
